package com.github.bigdata.sql.java.parser.spark;

import com.github.bigdata.sql.SqlParserAbstract;
import com.github.bigdata.sql.antlr4.spark.SparkSqlBaseLexer;
import com.github.bigdata.sql.antlr4.spark.SparkSqlBaseParser;
import com.github.bigdata.sql.enums.SqlTypeEnum;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;

/**
 * @ClassName MySparkSqlParser
 * @description:
 * @author: again
 * @Date: 2021/3/11 8:08 下午
 */
public class MySparkSqlParser extends SqlParserAbstract {

    private ParseTree getParseTree(String sql) {
        sql = sql.toUpperCase();
        CharStream input = CharStreams.fromString(sql);
        SparkSqlBaseLexer mySqlLexer = new SparkSqlBaseLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(mySqlLexer);
        SparkSqlBaseParser parser = new SparkSqlBaseParser(tokens);
        return parser.statement();
    }

    @Override
    public SqlTypeEnum parseSqlType(String sql) {
        SparkSqlTypeParser visitor = new SparkSqlTypeParser();
        visitor.visit(getParseTree(sql));
        return visitor.getSqlType();
    }

}
